*****************************************************************************
* Cleaning and Analyzing - PRITM countries. Alternative proxy				*
*   . Polarization proxy as Esteban and Ray 1994        *
*						Manifesto Project Database                          *
*  																            *
* Author: 			Valentina Gonzalez-Rostani		        				*
* Contact: 			mag384@pitt.edu              							*
* Date: 			March 21 2021 									        *
* Version:			Stata 17						                        *
*																			*
*****************************************************************************

/*
This do-file:
	A. Call the Data
	B. Define variables
	C. Export Tables 

Input: ** Manifesto Project database**
	- Data\CMP\MPDataset_MPDS2020a_stata14.dta // Data download from https://manifesto-project.wzb.eu/datasets 2021


Final output:
	Cleaned data: 
		* "Data\CMP_ER.dta" this data contains the relevant variables for the analysis with the DV as the polarization over redistribution, and fixed-value positions, estimated following Esteban and Ray 1994.
	Tables:
		* table A23: Partisan Polarization over Redistribution and Fixed Attributes


*/
cd "C:\Users\vgonz\Dropbox\Pitt\OneDrive for Business\Dissertation - Vale\Paper 2 - Political-Economic Polarization\Replication" // Only change your directory

* Processing of the data (alternatively skip and go to line 314)
{
*##########################################
* A. Calling the data
*##########################################
{

use "Data\CMP\MPDataset_MPDS2020a_stata14.dta", replace

}
*############################################
* B. Creating Variables
*############################################
{
* YEAR
gen year = year(edate) 

* Keep countries of interest
keep if countryname=="Australia" |  countryname=="Canada" |  countryname=="Greece" |  countryname=="New Zealand" |  countryname=="Portugal" |  countryname=="Spain" |  countryname=="United Kingdom" |  countryname=="United States" |  countryname=="France" |  countryname=="Norway" | countryname=="Austria" | countryname=="Belgium" | countryname=="Denmark" | countryname=="Estonia" | countryname=="Finland" | countryname=="Hungary" | countryname=="Germany" | countryname=="Iceland"  | countryname=="Ireland"  | countryname=="Italy"  | countryname=="Netherlands"  | countryname=="Norway" | countryname=="Slovakia" | countryname=="Slovenia" | countryname=="Sweden" | countryname=="Switzerland"

* Defines a dummy to identify PRITM countries
gen PRITM=. 
replace PRITM=0 if countryname=="Australia"
replace PRITM=0 if countryname=="Canada"
replace PRITM=0 if countryname=="Greece"
replace PRITM=0 if countryname=="New Zealand"
replace PRITM=0 if countryname=="Portugal"
replace PRITM=0 if countryname=="Spain"
replace PRITM=0 if countryname=="United Kingdom"
replace PRITM=0 if countryname=="United States"
replace PRITM=0 if countryname=="France"

replace PRITM=1 if countryname=="Austria"
replace PRITM=1 if countryname=="Belgium"
replace PRITM=1 if countryname=="Denmark"
replace PRITM=1 if countryname=="Estonia"
replace PRITM=1 if countryname=="Finland"
replace PRITM=1 if countryname=="Germany"
replace PRITM=1 if countryname=="Hungary"
replace PRITM=1 if countryname=="Iceland"
replace PRITM=1 if countryname=="Ireland"
replace PRITM=1 if countryname=="Italy"
replace PRITM=1 if countryname=="Netherlands"
replace PRITM=1 if countryname=="Norway"
replace PRITM=1 if countryname=="Slovenia"
replace PRITM=1 if countryname=="Slovakia"
replace PRITM=1 if countryname=="Sweden"
replace PRITM=1 if countryname=="Switzerland"


* CONTROL VARIABLES - number of parties

gen number = 1
egen number2= sum(number), by(edate)
lab var number2 "Number of parties"

*############################################################
* DEPENDENT VARIABLE Redistribution and Fixed Attributes
*############################################################
* Obtaining relevant policy variables
{
gen welfare_policy =  ln(per504+0.5) - ln(per505+0.5)

gen fixed = .
replace fixed = (ln(per107+per108+per407+per602+0.5+per602_2)-ln(per109+per110+per406+per601+0.5+per601_2)) if !missing(per602_2) & !missing(per601_2)
replace fixed = (ln(per107+per108+per407+per602+0.5)-ln(per109+per110+per406+per601+0.5)) if missing(per602_2) | missing(per601_2)

}
** Different code compared to 3_2 do file: 
{
**  Mainstream left: - soc social democratic and socialist or other left
gen m_left = . 
replace m_left = 1 if parfam== 30 | parfam== 20
replace m_left = 0 if parfam ~= 30 & parfam ~=20 & parfam ~=. 

egen m_left_participation= max(m_left), by(edate)


**  Mainstream left: only soc social democratic
gen m_left_restrict = . 
replace m_left_restrict = 1 if parfam== 30
replace m_left_restrict = 0 if parfam ~= 30 & parfam ~=. 

** Other left: Ecologista and socialist
gen o_left = . 
replace o_left = 1 if  parfam== 10  // here I am also including socialist or other left
replace o_left = 0 if parfam ~= 10  & parfam ~=. 

egen o_left_participation= max(o_left), by(edate)

** Mainstream right: lib liberal, Christian Democrat, Conservatives
gen m_right = . 
replace m_right = 1 if parfam == 40 | parfam == 50 | parfam == 60
replace m_right = 0 if parfam ~= 40 & parfam ~= 50 & parfam ~= 60 & parfam ~=. 

egen m_right_participation= max(m_right), by(edate)

** Other right: agriculture
gen o_right = . 
replace o_right = 1 if parfam == 80
replace o_right = 0 if parfam ~= 80 & parfam ~=. 

egen o_right_participation= max(o_right), by(edate)

** Radical right:  nat nationalist 
gen rad_right = . 
replace rad_right = 1 if parfam == 70 
replace rad_right = 0 if parfam ~= 70 & parfam ~=. 

egen rad_right_participation= max(rad_right), by(edate)


** Other parties: special issues and Ethnic and regional parties
gen o_parties = . 
replace o_parties = 1 if parfam == 90 | parfam == 95 
replace o_parties = 0 if parfam ~= 90 & parfam ~= 95  & parfam ~=. 

egen o_parties_participation= max(o_parties), by(edate)


** DISTANCE
{
foreach x of varlist welfare_policy  fixed  {

gen `x'_m_left = m_left*`x' if m_left==1
gen `x'_rad_right = rad_right*`x' if rad_right==1
gen `x'_m_right = m_right*`x' if m_right==1

gen `x'_o_left = o_left*`x' if o_left==1
gen `x'_o_right = o_right*`x' if o_right==1
gen `x'_o_parties = o_parties*`x' if o_parties==1

replace `x'_m_left = 0 if `x'_m_left ==.
replace `x'_rad_right = 0 if `x'_rad_right ==.
replace `x'_m_right = 0 if `x'_m_right ==.
replace `x'_o_left = 0 if `x'_o_left ==.
replace `x'_o_right = 0 if `x'_o_right ==.
replace `x'_o_parties = 0 if `x'_o_parties ==.
	
}

foreach x of varlist m_left rad_right m_right o_left o_right o_parties {

egen vote_`x' = mean(pervote), by(countryname edate `x')
replace vote_`x'=. if `x'==0
}



sort countryname edate
}
* Collapsing the data 
{
collapse (sum) fixed* welf*  (first) *_participation year  PRITM  totseats number2 oecdmember date (mean) vote_* , by(edate countryname)
}
// Final Prep of the data
{
egen country_number = group(countryname)

sort country_number edate
bysort country_number: gen election_order=_n
xtset country_number election_order


gen alpha=1.6




foreach x of varlist  welfare_policy fixed {

* Distance
// Mainstream left - other left
gen `x'_ml_ol =  vote_m_left^(alpha)*vote_o_left^(alpha)*abs(`x'_m_left -`x'_o_left)
replace `x'_ml_ol =  0 if o_left_participation==0

// Mainstream left - mainstream right
gen `x'_ml_mr =  vote_m_left^(alpha)*vote_m_right^(alpha)*abs(`x'_m_left -`x'_m_right)
replace `x'_ml_mr =  0 if m_right_participation==0

// Mainstream left - radical right
gen `x'_ml_rr =  vote_m_left^(alpha)*vote_rad_right^(alpha)*abs(`x'_m_left -`x'_rad_right)
replace `x'_ml_rr =  0 if rad_right_participation==0

// Mainstream left - other right
gen `x'_ml_or =  vote_m_left^(alpha)*vote_o_right^(alpha)*abs(`x'_m_left -`x'_o_right)
replace `x'_ml_or =  0 if o_right_participation==0

// Mainstream left - other parties
gen `x'_ml_op =  vote_m_left^(alpha)*vote_o_parties^(alpha)*abs(`x'_m_left -`x'_o_parties)
replace `x'_ml_op =  0 if o_parties_participation==0

//////
// Mainstream right - other left
gen `x'_mr_ol =  vote_m_right^(alpha)*vote_o_left^(alpha)*abs(`x'_m_right -`x'_o_left)
replace `x'_mr_ol =  0 if o_left_participation==0

// Mainstream right - radical right
gen `x'_mr_rr =  vote_m_right^(alpha)*vote_rad_right^(alpha)*abs(`x'_m_right -`x'_rad_right)
replace `x'_mr_rr =  0 if rad_right_participation==0

// Mainstream right - other right
gen `x'_mr_or =  vote_m_right^(alpha)*vote_o_right^(alpha)*abs(`x'_m_right -`x'_o_right)
replace `x'_mr_or =  0 if o_right_participation==0

// Mainstream right - other party
gen `x'_mr_op =  vote_m_right^(alpha)*vote_o_parties^(alpha)*abs(`x'_m_right -`x'_o_parties)
replace `x'_mr_op =  0 if `x'_mr_op==.


///// Radical right
// Radical right - other left
gen `x'_rr_ol =  vote_rad_right^(alpha)*vote_o_left^(alpha)*abs(`x'_rad_right -`x'_o_left)
replace `x'_rr_ol =  0 if o_left_participation==0

// Radical right - other right
gen `x'_rr_or =  vote_rad_right^(alpha)*vote_o_right^(alpha)*abs(`x'_rad_right -`x'_o_right)
replace `x'_rr_or =  0 if o_right_participation==0

// Radical right - other party
gen `x'_rr_op =  vote_rad_right^(alpha)*vote_o_parties^(alpha)*abs(`x'_rad_right -`x'_o_parties)
replace `x'_rr_op =  0 if o_parties_participation==0

///// Other left
// Other left - other right
gen `x'_ol_or =  vote_o_left^(alpha)*vote_o_parties^(alpha)*abs(`x'_o_left -`x'_o_right)
replace `x'_ol_or =  0 if o_right_participation==0

// Other left - other party
gen `x'_ol_op =  vote_o_left^(alpha)*vote_o_parties^(alpha)*abs(`x'_o_left -`x'_o_parties)
replace `x'_ol_op =  0 if o_parties_participation==0

//// Other right
// Other right- other party
gen `x'_or_op =  vote_o_right^(alpha)*vote_o_parties^(alpha)*abs(`x'_o_right -`x'_o_parties)
replace `x'_or_op =  0 if o_parties_participation==0

gen distance_`x' = `x'_ml_ol+ `x'_ml_mr+ `x'_ml_rr+ `x'_ml_or+ `x'_ml_op+ `x'_mr_ol+ `x'_mr_rr+ `x'_mr_or +`x'_mr_op+ `x'_rr_ol+ `x'_rr_or+ `x'_rr_op+ `x'_ol_or+ `x'_ol_op+ `x'_or_op

gen distance_`x'2 = `x'_ml_mr+ `x'_ml_rr+ `x'_ml_or+ `x'_mr_rr+ `x'_mr_or  + `x'_rr_or
gen distance_`x'3 = `x'_ml_mr+ `x'_ml_rr+ `x'_mr_rr  





}

gen shock=.
replace shock =0 if year > 1969 & year < 1995
replace shock =1 if  year > 1994	
}
}
}
*############################################
* Saving the data
*############################################
{

lab var PRITM "PR with Trichotomous Multipartism"
lab var totseats "Total Number of Seats"
lab var number2 "Total Number of Parties"
lab var oecdmember "OECD member"
lab var PRITM "PRITM"

lab var distance_welfare_policy3 "Distance Redistribution (DR) - Net Welfare"
lab var distance_fixed3 "Distance Fixed-Value Positions (DFVP) - Net Anti-Global"

lab var shock "High LMP period" // post 1994

keep  PRITM year  countryname  oecdmember totseats shock distance_welfare_policy3 distance_fixed3   number2 election_order country_number

keep if year>1969
keep if PRITM==1

save "Data\CMP_ER.dta", replace

}
}
* Alternatively load prepared data
{
use "Data\CMP_ER.dta", clear	
}
*##########################################
* Analysis
*##########################################
{

// table A23: Partisan Polarization over Redistribution and Fixed Attributes
{
preserve

eststo clear
eststo: qui reg distance_welfare_policy3 L.distance_welfare_policy3 shock totseats  oecdmember  number2 , cluster(countryname)
eststo: qui reg distance_fixed3 L.distance_fixed3 shock totseats  oecdmember  number2 , cluster(countryname)



esttab , replace label se title(Partisan Polarization over Redistribution and Fixed Attributes  \label {TabWithinER}) mti("Redistribution" "Fixed Values" "Redistribution" "Fixed Values") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock    ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("Control variables = tot*" "LDV = L.*" )

esttab using "Table\TabWithin_ER.tex", replace label se title(Partisan Polarization over Redistribution and Fixed Attributes  \label {TabWithinER}) mti("Redistribution" "Fixed Values" "Redistribution" "Fixed Values") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock    ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("Control variables = tot*" "LDV = L.*" )

restore
}
}